package org.toolbook.validator;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;
/**
 * 修改代码的验证器
 * @author meigang
 * @date 2015-06-26 15:05
 *
 */
public class UpdateCodeValidator extends Validator{

	@Override
	protected void validate(Controller c) {
		validateString("title", 1, 24, "title_info", "代码标题长度[1-24]");
		validateString("note", 1, 120, "note_info", "代码摘要长度[1-120]");
		validateRequired("content", "content_info", "代码内容不能为空");
		validateRequired("tags", "tags_info", "标签不能为空");
		//代码类型
		if(c.getPara("codeType").equals("null")){
			addError("codeType_info", "代码类型不能为空");
		}
		//标签格式
		String[] tags = c.getPara("tags").split(",");
		if(tags.length > 5){
			addError("tags_info", "标签个数最多5个");
		}
		String filePath = c.getPara("lookPhotoFilePath");
		if(filePath == null || filePath.equals("")){
			addError("lookPhotoFilePath_info", "缩览图未上传");
		}
	}

	@Override
	protected void handleError(Controller c) {
		// TODO Auto-generated method stub
		c.keepPara("id");
		c.keepPara("title");
		c.keepPara("note");
		c.keepPara("tags");
		c.keepPara("content");
		c.keepPara("codeType");
		c.keepPara("lookPhotoFilePath");
		c.render("/WEB-INF/pages/fc/code/update.jsp");
	}

}
